<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/7
 * Time: 13:07
 */

namespace Home\Controller;
use Lib\Api\GameTypeApi;
use Think\Controller;


class MinuteController extends Controller {
    public function index() {
        // $this->display();
    }

    public function getData() {
		$url = userState();
        if ($url != false) {
            $this->success('',$url);
        }
        $game_type = $_POST['type'];
        $map_time = $_POST['map_time'];

        $limit = 30;

        // 获取游戏开盘时间
        $gameType = GameTypeApi::getInfo(array("id"=>$game_type), "open_time");

        /*$date_list = array();
        $open_time = date("Y-m-d ").$gameType['open_time'];
        $open_time = date("Y-m-d H:i", strtotime("$open_time -1 minute"));
        $close_time = date("Y-m-d ", strtotime("+1 day")).$gameType['close_time'];
        $close_time = date("Y-m-d H:i", strtotime("$close_time +1 minute"));
        $this->getDateList($open_time, $close_time, $open_time, date("Y-m-d H:i", strtotime("$open_time +$map_time minute")), $map_time, $date_list);

        $time = date('Y-m-d H:i');
        $timeInterval = date("Y-m-d ", strtotime("+1 day")).$gameType['close_time'];
        for ($i=0; $i<count($date_list); $i++) {
            $begin_time = $date_list[$i]['begin_time'];
            $end_time = $date_list[$i]['end_time'];
            if ($time>$begin_time && $time <= date("Y-m-d H:i", strtotime("$end_time +1 minute"))) {
                $timeInterval = $date_list[$i]['end_time'];
            }
        }*/

        // $timeInterval = date('Y-m-d') . " " . $timeInterval;

        // $map_time = date('Y-m-d H:')+$map_time;
        /*if ((date('i')%$map_time) == 0) {
            $tt = 0;
        } else {
            if ($map_time > date('i')) {
                $tt = date('i');//$map_time - date('i');
            } else {
                $tt = $map_time-(date('i')%$map_time);
            }
        }*/
        $tt = $map_time-(date('i')%$map_time);
        // var_dump($tt);
        $time_i = $tt - 1 + date('i', strtotime($gameType['open_time']));
        $d_time = date('Y-m-d H:i');
        $timeInterval = date("Y-m-d H:i", strtotime("$d_time +$time_i minute"));
        // var_dump($time_i);
        $limit_date = array();
        for ($i=0; $i<$limit; $i++) {
            $d = $this->getDate(($map_time-1), $timeInterval);
            array_push($limit_date, $d);
        }

        sort($limit_date);
        $sql_list = array();
        for ($i=0; $i<count($limit_date); $i++) {
            $sql_temp = "select open_value, substring_index(group_concat(close_value order by create_date desc),',',1) close_value";
            $sql_temp .= ",max(max_value) as max_value, min(min_value) as min_value";
            // $sql_temp .= ",substring_index(group_concat(create_date order by create_date desc),',',1) time";
            $end_time = $limit_date[$i]['end_time'];
            $begin_time = $limit_date[$i]['begin_time'];

            $sql_temp .= ",'".date("Y-m-d H:i", strtotime("$begin_time +1 minute"))."' begin_time";
            $sql_temp .= ",'".date("Y-m-d H:i", strtotime("$end_time +1 minute"))."' time";
            $sql_temp .= " from fs_minute ";
            $sql_temp .= "where game_type =".$game_type." and (DATE_FORMAT(create_date, '%Y-%m-%d %H:%i') >= '".$limit_date[$i]['begin_time']."' and DATE_FORMAT(create_date, '%Y-%m-%d %H:%i') <= '".$limit_date[$i]['end_time']."')";
            array_push($sql_list, $sql_temp);
        }

        $sql = "select * from (".implode(" union all ", $sql_list).") t order by t.time asc limit ".$limit;
        $model = new \Think\Model();
        $list = $model->query($sql);
        $ajaxdata["list"] = $list;
        $this->ajaxReturn($ajaxdata);
    }

    private function getDate($map_time, &$end_time) {
        $begin_time = date("Y-m-d H:i", strtotime("$end_time -$map_time minute"));
        $d['begin_time'] = $begin_time;
        $d['end_time'] = $end_time;
        $end_time = date("Y-m-d H:i", strtotime("$begin_time -1 minute"));
        return $d;
    }

    private function getDateList($open_time, $close_time, $begin_time, $end_time, $map_time, &$date_list) {
        if (strtotime($open_time) <= strtotime($begin_time) && strtotime($close_time) >= strtotime($end_time)) {
            $d['begin_time'] = date("Y-m-d H:i", strtotime("$begin_time +1 minute"));
            $d['end_time'] = date('Y-m-d H:i', strtotime($end_time));
            array_push($date_list, $d);
            $this->getDateList($open_time, $close_time, $end_time, date("Y-m-d H:i", strtotime("$end_time +$map_time minute")),$map_time, $date_list);
        }
    }
}